//
//  YNNetworkManager.m
//  YNDemo
//
//  Created by 胡锦涛 on 2020/11/15.
//

#import "HKNetworkManager.h"
#import "ZBNetworking.h"
@interface HKNetworkManager()
/** 下载数据收集 :仅用于下载*/
@property (nonatomic, strong) NSMutableArray * downloads ;
@end
@implementation HKNetworkManager
+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    static HKNetworkManager *instance = nil;
    dispatch_once(&onceToken,^{
        instance = [[super allocWithZone:NULL] init];
    });
    return instance;
}

+ (id)allocWithZone:(struct _NSZone *)zone{
    return [self sharedInstance];
}

- (void)sendGetRequestWithUrl:(NSString *)url withParameters:(NSMutableDictionary *)parameters success:(YNRequestSuccessBlock)success failure:(YNRequestFailureBlock)failure{
//    NSString *url = @"https://wanandroid.com/article/listproject/0/json";
    [ZBRequestManager requestWithConfig:^(ZBURLRequest * _Nullable request) {
        request.url=url;
        request.methodType=ZBMethodTypeGET;//默认为GET
        request.apiType=ZBRequestTypeRefresh;//（默认为ZBRequestTypeRefresh 不读取缓存，不存储缓存）
        request.requestSerializer=ZBJSONRequestSerializer; //单次请求设置 请求格式 默认JSON，优先级大于 公共配置，不影响其他请求设置
        request.responseSerializer=ZBJSONResponseSerializer; //单次请求设置 响应格式 默认JSON，优先级大于 公共配置,不影响其他请求设置
        request.timeoutInterval=10;//默认30 //优先级 高于 公共配置,不影响其他请求设置
        request.parameters = parameters;
    } success:^(id  _Nullable responseObject, ZBURLRequest * _Nullable request) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            NSDictionary *dict = (NSDictionary *)responseObject;
//            QMUILog(@"ZBRequestManager",@"requestGet%@",dict);
            if (success) {
                success(dict);
            }
        }else {
            if (failure) {
                failure(@"Server返回数据格式不正确✅");
            }
        }
    } failure:^(NSError * _Nullable error) {
        if (failure) {
            failure(error.localizedFailureReason);
        }
    }];
}
- (void)sendPostRequestWithUrl:(NSString *)url withParameters:(NSMutableDictionary *)parameters success:(YNRequestSuccessBlock)success failure:(YNRequestFailureBlock)failure{
    [ZBRequestManager requestWithConfig:^(ZBURLRequest * _Nullable request) {
        request.url=url;
        request.methodType=ZBMethodTypePOST;//默认为GET
        request.apiType=ZBRequestTypeRefresh;//（默认为ZBRequestTypeRefresh 不读取缓存，不存储缓存）
        request.requestSerializer=ZBJSONRequestSerializer; //单次请求设置 请求格式 默认JSON，优先级大于 公共配置，不影响其他请求设置
        request.responseSerializer=ZBJSONResponseSerializer; //单次请求设置 响应格式 默认JSON，优先级大于 公共配置,不影响其他请求设置
        request.timeoutInterval=10;//默认30 //优先级 高于 公共配置,不影响其他请求设置
        request.parameters = parameters;
    } success:^(id  _Nullable responseObject, ZBURLRequest * _Nullable request) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            NSDictionary *dict = (NSDictionary *)responseObject;
//            QMUILog(@"ZBRequestManager",@"requestGet%@",dict);
            if (success) {
                success(dict);
            }
        }else {
            if (failure) {
                failure(@"Server返回数据格式不正确✅");
            }
        }
    } failure:^(NSError * _Nullable error) {
        if (failure) {
            failure(error.localizedFailureReason);
        }
    }];
}
- (void)sendDownloadRequestWithUrl:(NSString *)url withParameters:(NSMutableDictionary *)parameters withProgress:(YNRequestProgressBlock)progressBlock success:(YNRequestSuccessBlock)success failure:(YNRequestFailureBlock)failure{
    
    if (!self.downloads) {
        self.downloads = @[].mutableCopy;
    }
    if ([self.downloads containsObject:url]) {
        self.isRepeatedDownload = YES;
    }else {
        [self.downloads addObject:url];
        self.isRepeatedDownload = NO;
    }
    
    [ZBRequestManager requestWithConfig:^(ZBURLRequest * _Nullable request) {
        request.url=url;
        request.methodType=ZBMethodTypeDownLoad;
        request.apiType=ZBRequestTypeRefresh;//（默认为ZBRequestTypeRefresh 不读取缓存，不存储缓存）
        request.requestSerializer=ZBJSONRequestSerializer; //单次请求设置 请求格式 默认JSON，优先级大于 公®共配置，不影响其他请求设置
        request.responseSerializer=ZBJSONResponseSerializer; //单次请求设置 响应格式 默认JSON，优先级大于 公共配置,不影响其他请求设置
        request.timeoutInterval=10;//默认30 //优先级 高于 公共配置,不影响其他请求设置
        request.parameters = parameters;
    } progress:^(NSProgress * _Nullable progress) {
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{   //下载过程中由多个线程返回downloadProgress，无法给progress赋值进度，所以要选出主线程
            progressBlock(progress);
        }];
    } success:^(id  _Nullable responseObject, ZBURLRequest * _Nullable request) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSError * _Nullable error) {
        if (failure) {
            failure(error.localizedFailureReason);
        }
    } finished:^(id  _Nullable responseObject, NSError * _Nullable error, ZBURLRequest * _Nullable request) {
//        QMUILog(@"YNNetworkManager",@"下载完成了==>%@",responseObject);
        
    }];
    
}
@end
